//! Controller for manipulate diagram objects
Ext.define('UmlWebEditor.controller.DiagramController', {
	extend: 'Ext.app.Controller',
	//requires: ['UmlWebEditor.view.factories.BlocksFactory'],
	views: [
	'CanvasWidget',
	'ToolsCollection',
	'Edit'
	],
	stores: [
	'UsersStore'
	],
	models: [
	'UserModel'
	],
	blocksOnCanvas: new Ext.util.MixedCollection(),
	tools: new Ext.util.MixedCollection(),
	init: function() {
		this.control({
			'CanvasWidget': {
				add: this.onAddBlockToCanvas,
				afterrender: this.onDiagrammWidgetRendered,
				addElementToDiagramm: this.onAddElementToDiagramm
			},
			'ToolsCollection': {
				addToolToToolsCollection: this.onAddToolToToolsCollection
			}
		});
	},
	onDiagrammWidgetRendered: function(view, addedComponent) {
		console.log('onDiagrammWidgetRendered view.getXType()=' + view.getXType());
		this.getStore('UsersStore').each(function(item){
			console.log('each items' + item.get('xtype') + view);
			view.add([{
				 xtype: item.get('xtype'),
				 x: item.get('x'),
				 y: item.get('y')
			}]);
		});
	},
	onAddBlockToCanvas: function(sender, addedComponent) {
		console.log('DiagramController onAddBlockToCanvas id=' + addedComponent.getId());
		//this.blocksOnCanvas.add(addedComponent.getId(), addedComponent);
	},
	onAddToolToToolsCollection: function(sender, addedComponent) {
		console.log('DiagramController onAddBlockToToolsCollection id=' + addedComponent.getId());
		this.tools.add(addedComponent.getId(), addedComponent);
	},
	onAddElementToDiagramm: function (sender, x, y, id) {
		console.log('onAddElementToDiagramm: dd.id=' + id);
		var item = this.tools.get(id);
		var offsetX = 0;
		var offsetY = 0;
		if(item)
		{
			/*console.log('onAddElementToDiagramm: item is present in tools');
			var xtype = 'widget.' + item.xtype;
			offsetX = item.offsetX;
			offsetY = item.offsetY;
			item = Ext.create(xtype);
			sender.add(item);
			item.setPosition(x - offsetX, y - offsetY);*/
		} else {
			console.log('onAddElementToDiagramm: item no present in tools');
			/*item = this.blocksOnCanvas.get(id);
			if(item) {
				console.log('onAddElementToDiagramm: item is present in blocks');
				offsetX = item.offsetX;
				offsetY = item.offsetY;
				item.setPosition(x - offsetX, y - offsetY);
			} else
			console.log('onAddElementToDiagramm: item no present in blocks');
			*/
		}
	}
});
